package com.licenta.fbApp.client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import com.licenta.fbApp.shared.Restaurant;
import com.licenta.fbApp.client.LoadableTab;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TipsPageTab  extends Composite implements LoadableTab {

	private final VerticalPanel panel = new VerticalPanel();
	private final VerticalPanel resultPanel = new VerticalPanel();	
	private boolean loaded = false;	

	private ArrayList<Restaurant> result = new ArrayList<Restaurant>();
	private HashMap<Integer, String> likes  = new HashMap<Integer, String>();
	
	private int TOTAL;
	private static int CURRENT = 0;
	private static int STEP = 5;
	private HTML[] rests = new HTML[STEP];
	private final HorizontalPanel buttons = new HorizontalPanel();
	private final Button loadNext = new Button(" -> ");
	private final Button loadPrev = new Button(" <- ");
	
	private boolean canLoad = false;
	
	public TipsPageTab() {	
				
		initWidget(panel);
	    addStyleName("tabContentStyle");
	  }
	
	public void load() {
		if (loaded) {
		      return;
		    }	
		    loaded = true;	    
		    panel.add(new HTML("<br />   Aici puteti gasi recomandari personalizate propuse <br /> "));
		    
		    buttons.add(loadPrev);
		    loadPrev.setTitle("Anterior");
		    loadPrev.addClickHandler(new ClickHandler() {
		        
		        public void onClick(ClickEvent event) {
		          loadNext(false);
		        }
		      });
		    
		    buttons.add(loadNext);
		    loadNext.setTitle("Urmator");
		    loadNext.addClickHandler(new ClickHandler() {
		        
		        public void onClick(ClickEvent event) {
		          loadNext(true);
		        }
		      });
		    
		    panel.add(buttons);
		    panel.add(resultPanel);
	}
	
	  public void set(ArrayList<Restaurant> results) {
			this.result = results;
			this.TOTAL = result.size();
	  }
	  
	 private void add()
	  {			
		  int i;
		  Restaurant s;	
		  String html;
		  String sim = "";
		 
	        for (i = CURRENT; i < (CURRENT + STEP)%TOTAL; i++) {
	        	s = result.get(i);	
	        	html = "<h2>" + s.name + "</h2>";
	        	html += "<div style=\"color: #bbbbbb; font-weight: 13px;\"> Restaurante similare : <br />";
	        	sim = getSimilar(i);	        	
	        	html += sim;
	        	html = html.substring(0, html.length() - 3);
	        	html += "</div>";
	        	rests[i-CURRENT] = new HTML(html);
	        	resultPanel.add(rests[i-CURRENT]);
	        }
	  }
	 
	 public void setLikes(HashMap<Integer, String> res) {		 
		 for (Integer k : res.keySet()) {
			 likes.put(k, res.get(k));	
		 }
		 if (likes.size() > 500)
			 add();
	}
	 
	 private String getSimilar(int item) {
		 int i;
		 String res = "", value;
		 String[] tokens;
		 
		 if (likes != null && likes.containsKey(item)) {
			 value = likes.get(item);
			 System.out.println(result.get(item).name +  " " + item + " " + value);
			 if (value.length() > 1 && value.contains(" ")) {
				 tokens = value.split(" ");
				 for (i = 1; i < tokens.length && i < 12; i++) {
					 res += result.get(Integer.parseInt(tokens[i])).name + ", ";
				 }
			 }
			 System.out.println(" " + res);
		 }
		 return res;
	 }
	 
	  private void loadNext(boolean more) 
	  {
		  
		  loadNext.setEnabled(false);
		  loadPrev.setEnabled(false);		  
		  if ( more ) {
			  CURRENT += STEP;
			  CURRENT %= TOTAL;
		  } else {
			  CURRENT -= STEP;
			  if (CURRENT < 0 ) {
				  CURRENT = 0;
			  }
		  }		System.out.println(CURRENT);
		  
		  resultPanel.clear();
		  add();
		  loadNext.setEnabled(true);
		  loadPrev.setEnabled(true);
	  }
	 
}
